<?php
include_once 'DbTools.php';

$moneyOut = $_POST['moneyOut'];
$money = '';
$moneyInCard = $_POST['moneyInCard'];



DbTools::InitDb();




session_start();
$userInfo = $_SESSION[$_COOKIE['Token']];
$userCardId = $userInfo['CardId'];

//查询操作用户卡中卡的金额
$sql_findUseCardMoney = "select CardMoney from bankcard where CardId = ".$userCardId;
$result = DbTools::select();
$money = $result[0]['CardMoney'];
//如果转出金额大于原本的余额则跳回原本的页面
if ($money < $moneyOut){
    echo '<script>
        alert("超出余额")
        history.back()
    </script>';
    die;
}


//更改用户卡中的金额数据
$sql = "upset bankcard set CardMoney = CardMoney - '$moneyOut' where CardId = '$userCardId'";
$result = DbTools::noSelect($sql);
//将金额转入其他卡中
$sql2 = "upset bankcard set CardMoney = CardMoney + '$moneyOut' where CardNo = '$moneyInCard'";
$result2 =DbTools::noSelect($sql2);

$backUrl ='';
if ($result['status']==1 && $result2['status']==1){
    $backUrl = '../user/userInfo.php';
}

//关闭数据库连接
DbTools::close();


?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

</body>
<script>

    <?php if ($result['status']=='true'): ?>
    alert('取出成功');
    location.href = <?php echo $backUrl;?>
    <?php endif; ?>
</script>
</html>
